package Application.Tool;

import Application.AppJoint;
import Application.Controller.MainWindowController;
import Application.Controller.PenSettingsController;
import Application.Mask.MaskManager;
import Application.StatusBarManager.StatusBarManager;
import javafx.scene.Parent;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.ColorPicker;
import javafx.scene.paint.Color;
import javafx.scene.control.ComboBox;
import Application.LayerImage.LayerImage;
import javafx.scene.input.MouseEvent;
import Application.Layer.Layer;
import Application.Layer.LayerDelegate;

/**
 * 画笔工具类，singleton
 */
public class NormalPen extends Tool {
    // 工具设置界面
    private Parent root;

    private static NormalPen ins = new NormalPen();
    //private int flag = 0;

    // 大小
    private int size = 32;
    // 颜色
    //private Color color;
    //private int color;
    private int color = 0xFFFFFF;
    // 蒙版
    private LayerImage mask;
    // 不透明度
    private int opacity = 255;

    // c1 覆盖于 c2 之上
    private int blend(int c1, int c2) {
        int ans = 0;
        int a1 = (c1 >> 24) & 0xff;
        int r1 = (c1 & 0x00ff0000) >> 16;
        int g1 = (c1 & 0x0000ff00) >> 8;
        int b1 = (c1 & 0x000000ff);
        int a2 = (c2 >> 24) & 0xff;
        int r2 = (c2 & 0x00ff0000) >> 16;
        int g2 = (c2 & 0x0000ff00) >> 8;
        int b2 = (c2 & 0x000000ff);

        int a = a1 + a2;
        if(a > 255) {
            a = 255;
        }

        int r = (a1 * r1 + (255 - a1) * r2) / 255;
        int g = (a1 * g1 + (255 - a1) * g2) / 255;
        int b = (a1 * b1 + (255 - a1) * b2) / 255;

        ans = (a << 24) | (r << 16) | (g << 8) | b;

        return ans;
    }


    //
    private MainWindowController mainWindowController;

    private NormalPen(){
        super();
        // 加载本工具的工具设置
        try {
//            root = FXMLLoader.load(getClass().getResource("../View/PenSettings.fxml"));
            FXMLLoader loader = new FXMLLoader(getClass().getResource("/View/PenSettings.fxml"));
//            FXMLLoader loader = new FXMLLoader(getClass().getResource("Application/View/PenSettings.fxml"));
            root = loader.load();

            ComboBox<String> combo = ((PenSettingsController)(loader.getController())).getMaskSettings();
            AppJoint.getInstance().loadMasksToComboBox(combo);

            PenSettingsController penSettingsController =  loader.getController();
            mainWindowController = AppJoint.getInstance().getMainWindowController();
            //mainWindowController.setColorPicker(penSettingsController.geler()tPenSettingsColor());

            ColorPicker colorPicker = penSettingsController.getPenSettingsColor();

            mainWindowController.setColorPicker(colorPicker);
        } catch(Exception e) {
            //e.printStackTrace();
            StatusBarManager.getInstance().setText(e.toString());
        }

        //mask = MaskManager.getInstance().getMaskByName("src/Application/Assets/masks/test.png");
        mask = MaskManager.getInstance().getMaskByName("test");
        //AppJoint.getInstance().getImageView().setImage(mask.getWritableImage());
    }

// Settings **********************
    public void setSize(int size) {
        this.size = size;
        //System.out.println(size);
    }

    public void setColor(Color color) {
        //this.color = color
    }

    public void setMask(String maskName) {
        mask = MaskManager.getInstance().getMaskByName(maskName);
    }

    public void setColor(int a, int r, int g, int b) {
        this.color = (a << 24) | (r << 16) | (g << 8) | b;
        //System.out.println("color" + this.color);
    }

    public void setOpacity(int opacity) {
        this.opacity = opacity;
    }

// *******************************

    // 获取工具设置界面

    @Override public Parent getToolSettings() {
        return root;
    }

    @Override public void onMouseDown(MouseEvent e) {
// 点击在imageview中的位置在实际图片中的像素位置
        onMouseMove(e);
    }

    @Override public void onMouseMove(MouseEvent e) {
        // 点击在imageview中的位置在实际图片中的像素位置


        int activeid = AppJoint.getInstance().getActiveLayerId();
        if(activeid==-1){
            //System.out.println("你必须新建一个图层！");
            //Pen.getInstance().getMain().showWarnningWindow("你必须新建一个图层！");
            //StatusBarManager.getInstance().setText("你必须新建一个图层！");
        }else{
            double pixelx =
                    e.getX() / AppJoint.getInstance().getImageView().getFitWidth() * AppJoint.getInstance().getCanvasWidth();
            double pixely =
                    e.getY() / AppJoint.getInstance().getImageView().getFitHeight() * AppJoint.getInstance().getCanvasHeight();
            Layer activeLayer = LayerDelegate.getInstance().getLayerById(AppJoint.getInstance().getActiveLayerId());
            for(int i = -size / 2; i < size / 2; i++) {
                for(int j = -size / 2; j < size / 2; j++) {
                    // 实际像素位置
                    int imgx = (int)pixelx + i;
                    int imgy = (int)pixely + j;
                    if(imgx >= 0 && imgx < AppJoint.getInstance().getCanvasWidth() && imgy >= 0 && imgy < AppJoint.getInstance().getCanvasHeight()) {
                        // 蒙版像素位置
                        int maskx = (i + size / 2) * MaskManager.MASK_SIZE / size;
                        int masky = (j + size / 2) * MaskManager.MASK_SIZE / size;
                        // 蒙版灰度
                        int gray = (mask.getPixelArgb(maskx, masky) & 255);
                        int alpha = gray * this.opacity / 255;

                        int oldColor = activeLayer.getArgb(imgx, imgy);
                        int srcColor = (alpha << 24) | (this.color & 0x00ffffff);

                        int finalColor = this.blend(srcColor, oldColor);

                        activeLayer.setArgb(imgx, imgy, finalColor);
                        LayerDelegate.getInstance().refreshSingle(imgx, imgy);
                    }
                }
            }
        }
    }

    @Override public void onMouseUp(MouseEvent e) {
        //flag = 0;
    }

    public static NormalPen getInstance() {
        return ins;
    }

}
